var app = getApp()
function formatTime(date) {
  var year = date.getFullYear()
  var month = date.getMonth() + 1
  var day = date.getDate()

  var hour = date.getHours()
  var minute = date.getMinutes()
  var second = date.getSeconds()


  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}
function formatDate(date) {
  var year = date.getFullYear()
  var month = date.getMonth() + 1
  var day = date.getDate()


  return [year, month, day].map(formatNumber).join('-')
}

function formatNumber(n) {
  n = n.toString()
  return n[1] ? n : '0' + n
}
function checktime(obj){
  var current = new Date()
  var currenttime = current.getTime()
  var objd = new Date(obj)
  var objtime = objd.getTime()
  if(objtime>currenttime){
    return -1
  }else{
    return 1
  }
}
const productstatus= [
      {status:1,name:'未开始'},
      {status:2,name:'进行中'},
      {status:3,name:'终止'},
      {status:4,name:'结束'}
    ]
function getSelectmanage(id){
  var manage = app.manageslist.find(function(item){
    return item.id == id
  })
  return manage && manage.name
}
function getSelectmanageid(name){
  var manage = app.manageslist.find(function(item){
    return item.name == name
  })
  return manage && manage.id
}
function getSelectteamleaderslist(select){
  var selects = select.split(',')
  var teamleaders = JSON.parse(JSON.stringify(app.teamleaderslist));
  teamleaders.forEach(function(item){
    item.checked = false
    if(select.indexOf(item.id)!=-1){
      item.checked = true
    }
  })
  return teamleaders
}
function getSelectteamleaderslistJoin(select){
  var selects = select.split(',')
  var temp = []
  var teamleaders = JSON.parse(JSON.stringify(app.teamleaderslist));
  teamleaders.forEach(function(item){
    if(select.indexOf(item.id)!=-1){
     temp.push(item.name)
    }
  })
  return temp.join()
}
function getstatus(status){
   var obj =productstatus.find(function(item){
    return item.status == status
  })
  return obj.name
}
function isEmpty(obj){
	return Object.keys(obj).length === 0 && obj.constructor === Object
}
function showloading(){
  wx.showLoading && wx.showLoading({title: '加载中'})
}
function hideloading(){
  wx.hideLoading && wx.hideLoading()
}
module.exports = {
  formatTime: formatTime,
  formatDate: formatDate,
  checktime:checktime,
  productstatus:productstatus,
  getSelectmanage:getSelectmanage,
  getSelectmanageid:getSelectmanageid,
  getSelectteamleaderslist:getSelectteamleaderslist,
  getSelectteamleaderslistJoin:getSelectteamleaderslistJoin,
  getstatus:getstatus,
  isEmpty:isEmpty,
  showLoading:showloading,
  hideLoading:hideloading
}
